home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Scheduling / Cassandra / Source / TextWindow.m < prev    next >
Encoding:
Text File  |  1991-04-15  |  4.1 KB  |  222 lines

  1. //
  2. // TextWindow.m
  3. // Copyright (c) 1990 by Jiro Nakamura 
  4. // All rights reserved
  5. //
  6. // Handles a window with a scroll view in it, opens it with a filename,
  7. // lets the user modify it,
  8. //  and if anything changes, lets she or he save it before
  9. // closing it.
  10. //
  11. //    by Jiro Nakamura (ac6y@vax5.cit.cornell.edu)
  12. //
  13. // RCS Information
  14. // Revision Number->    $Revision: 2.15 $
  15. // Last Revised->    $Date: 90/12/03 01:56:35 $
  16. //
  17. static char rcsid[] = "$Id: TextWindow.m,v 2.15 90/12/03 01:56:35 jiro Exp Locker: jiro $";
  18.  
  19. #import "TextWindow.h"
  20. #import <sys/file.h>
  21. #import <appkit/ScrollView.h>
  22. #import <appkit/Text.h>
  23. #import <appkit/Window.h>
  24. #import "cass.h"            // for default FONTSIZE and FONTNAME            
  25. #import <libc.h>            /* for open and close */
  26. #import <appkit/Panel.h>    /* for NXRunAlertPanel */
  27.  
  28. @implementation TextWindow
  29. + new
  30. {
  31.     self = [super new];
  32.     filename = "";                // Default values
  33.     windowIcon = "";
  34.     [self setTitle: "Window"];
  35.     return self;
  36. }
  37.  
  38. - free
  39. {
  40.     [self performClose : self];
  41.     [super free];
  42.     return self;
  43. }
  44.  
  45. - setFilename: (const char *) file;
  46. {
  47.     filename = file;
  48.     return self;
  49. }
  50.  
  51. - setWindowIcon: (const char *) name;
  52. {
  53.     windowIcon = name;
  54.     return self;
  55. }
  56.  
  57.  
  58. - (const char *) filename
  59. {
  60.     return filename;
  61. }
  62.  
  63. - (const char *) windowIcon
  64. {
  65.     [self setMiniwindowIcon: windowIcon];
  66.     return windowIcon;
  67. }
  68.  
  69. - openWith: (const char *) file
  70. {
  71.     filename =     file;
  72.     textView =     [textScroll docView];
  73.     [textView     setDelegate : self];
  74.     [textView    setMonoFont: NO];
  75.     [self         setMiniwindowIcon: windowIcon];
  76.  
  77.     /* If we are not visible, then we need to do an update after */
  78.     /* making ourselves the key window. */
  79.     if( ![self isVisible])        
  80.         {
  81.         [self makeKeyAndOrderFront : self];
  82.         [self update];
  83.         }
  84.     else
  85.         [self makeKeyAndOrderFront : self];
  86.  
  87.     return self;
  88. }
  89.  
  90. - close
  91. {    
  92.     #ifdef DEBUG
  93.         fprintf(stderr,"Window <%s> about to close. Checking save.\n"
  94.                 "IsDocEdited = %s, IsVisible = %s.\n",
  95.                 [self title],
  96.                 [self isDocEdited]?"Yes":"No",
  97.                 [self isVisible]?"Yes":"No");
  98.     #endif
  99.     [self save];
  100.     [self setDocEdited: NO];
  101.     [textView becomeFirstResponder];
  102.     if( [self isVisible] )
  103.         [super close];
  104.     return self;
  105. }
  106.  
  107. - save
  108. {
  109.     int fd;
  110.     NXStream    *output; 
  111.         
  112.     if( [self isDocEdited] || [self isVisible] )
  113.         {
  114.         // If the text is zero length then let us delete it.
  115.         if( [textView textLength] == 0)
  116.             {
  117.             #ifdef DEBUG
  118.                 fprintf(stderr, "Deleting %s because "
  119.                         "it's empty.\n",
  120.                         filename);
  121.             #endif
  122.             unlink(filename);
  123.             }
  124.         else
  125.             {
  126.             #ifdef DEBUG
  127.                 fprintf(stderr, "Saving %s.\n",
  128.                     filename);
  129.             #endif
  130.             // Create and truncate file to zero
  131.             fd = open(filename, O_TRUNC | O_CREAT, 0600);
  132.             close(fd);
  133.             output = NXMapFile(filename, NX_WRITEONLY);
  134.             [textView writeRichText:output]; 
  135.             NXSaveToFile(output, filename);
  136.             NXCloseMemory(output, NX_FREEBUFFER); 
  137.             [self setDocEdited: NO];
  138.             [textView becomeFirstResponder];
  139.             }
  140.         }
  141.     return self;
  142. }
  143.  
  144.  
  145. // Re-read the file in and display it on the view (if it is visible, that is)
  146. - update
  147. {
  148.     if( [self isVisible])
  149.         {
  150.         NXStream    *input; 
  151.  
  152.         if( (input = NXMapFile( filename, NX_READONLY)) == NULL)
  153.             {
  154.             [textView selectAll: self];
  155.             [textView setText: ""];
  156.             [textView setSel: 0 : 0];
  157.             }    
  158.         else
  159.             {
  160.             [textView readRichText:input]; 
  161.             NXCloseMemory(input, NX_FREEBUFFER); 
  162.             }
  163.             
  164.         [self setDocEdited: NO];
  165.         [textView setSel: 0 : 0];
  166.         }
  167.     return self;
  168. }
  169.  
  170. - printPSCode: sender
  171. {
  172.     [textView printPSCode: self];
  173.     return self;
  174. }
  175.  
  176. // This moves the current selection to the end of the text
  177. - selectTextEnd: sender
  178. {
  179.     [textView setSel: [textView textLength] : [textView textLength]]; 
  180.     [textView scrollSelToVisible];
  181.     return self;
  182. }
  183.  
  184.  
  185. // This is a delegate method. We are our own delegate.
  186. // Let's us change the close button's shape if we have
  187. // been edited.
  188. - textDidChange : object
  189. {
  190.     [self setDocEdited: YES];
  191.     [textView becomeFirstResponder];
  192.     return self;
  193. }
  194.  
  195. - textWillConvert:textObject  fromFont:from toFont:to
  196. {
  197.     [self setDocEdited: YES];
  198.     [textView becomeFirstResponder];
  199.     return to;
  200. }
  201.  
  202. - setDocEdited: (BOOL) flag
  203. {
  204.     #ifdef AUTOSAVE    
  205.         hasChanged = flag;
  206.         return self;
  207.     #else
  208.         return( [super setDocEdited: flag]);
  209.     #endif
  210. }
  211.  
  212. - (BOOL) isDocEdited
  213. {
  214.     #ifdef AUTOSAVE    
  215.         return hasChanged;
  216.     #else
  217.         return( [super isDocEdited]); 
  218.     #endif
  219.     
  220. }
  221. @end
  222.